import datetime
from typing import List, Dict

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, Table, ForeignKey, text, JSON, TEXT, distinct
from sqlalchemy.orm import Session, relationship

from common.database import Base
from common.database import SessionMixin


class ToolsShortenedUrl(Base, SessionMixin):
    __tablename__ = 'tools_shortened_urls'

    original_url = Column(TEXT, nullable=False, comment="原始网址")
    short_link = Column(String(50), nullable=False, comment="短地址")
    create_time = Column(DateTime, default=datetime.datetime.now, nullable=False, comment="创建时间")

    __table_args__ = (
        Index('idx_tools_shortened_urls_short_link', 'short_link'),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic',
        }
    )

    def __repr__(self):
        return f"<uuid='{self.uuid}',original_url='{self.original_url}',short_link='{self.short_link}'>"

    @classmethod
    def get_paginate(cls, db: Session, page, per_page):
        cls.paginate = db.query(cls).with_entities(cls.uuid, cls.original_url, cls.short_link,
                                                   cls.create_time).paginate(page=page, per_page=per_page)
        return cls.paginate
